﻿<div class="page-container" id="admui-pageContent">
    <div class="page animation-fade page-forms">
        <div class="page-header">
            <h1 class="page-title">小区修改</h1>
            <ol class="breadcrumb">
                <li class="breadcrumb-item">
                    <a class="icon wb-home" href="javascript:;">小区管理</a>
                </li>
                <li class="breadcrumb-item active">小区修改</li>
            </ol>
        </div>
        <div class="page-content">
            <div class="panel">
                <div class="panel-body">
                    <div class="example-wrap">
                        <div class="example">
                            <div class="form-group">
                                <label class="control-label" for="inputInlineUsername">小区名称</label>
                                <input type="text" class="form-control" id="xiaoqumingcheng" name="xiaoqumingcheng" placeholder="战区名称" autocomplete="off" />
                            </div>
                            <div class="form-group">
                                <div class="col-sm-1">
                                    <input type="button" id="TianJia" onclick="Update()" class="btn btn-block btn-info" value="修改" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="page-content" id="zhanduixiaoqupeizhi">
            <div class="panel">
                <div class="panel-body">
                    <div class="example-wrap">
                        <div class="example">
                            <div class="form-group">
                                <label class="control-label" for="inputInlineUsername">战队名称</label>
                                <select class="form-control" id="zhanduimingcheng"></select>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-12">
                                    <input type="button" id="guanlitianjia" onclick="AddGuanLian()" class="btn btn-block btn-info" value="添加" />
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-12">
                                    <table class="table table-bordered table-hover dataTable table-striped w-full text-nowrap" id="dataTableExample" data-plugin="dataTable">
                                        <thead>
                                            <tr>
                                                <th>序号</th>
                                                <th>小区名称</th>
                                                <th>战区名称</th>
                                                <th>操作</th>
                                            </tr>
                                        </thead>
                                        <tfoot>
                                            <tr>
                                                <th>序号</th>
                                                <th>小区名称</th>
                                                <th>战区名称</th>
                                                <th>操作</th>
                                            </tr>
                                        </tfoot>
                                        <tbody></tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <input type="text" hidden="hidden" id="fangyuaniid" />
    </div>
</div>
<script>
    $(document).ready(function () {
        $("#SpanFangWu").click();
        var shu = getQuery("id");
        $("#fangyuaniid").val(shu);
        zhanqu();
        morenzhi();
    });

    function bang() {
        var id = $("#fangyuaniid").val();
        var lang = {
            "sProcessing": "大量处理中，请稍后...",
            "sLengthMenu": "每页 _MENU_ 项",
            "sZeroRecords": "没有匹配结果",
            "sInfo": "当前显示第 _START_ 至 _END_ 项，共 _TOTAL_ 项。",
            "sInfoEmpty": "当前显示第 0 至 0 项，共 0 项",
            "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
            "sInfoPostFix": "",
            "sSearch": "搜索:",
            "sUrl": "",
            "sEmptyTable": "表中数据为空",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页",
                "sJump": "跳转"
            },
            "oAria": {
                "sSortAscending": ": 以升序排列此列",
                "sSortDescending": ": 以降序排列此列"
            }
        };
        $("#dataTableExample").dataTable().fnDestroy();
        $('#dataTableExample').dataTable({
            "order": [[0, "asc"]],
            "language": lang,
            "processing": true,
            "serverSide": true,
            //"bStateSave": true,//状态保持模式
            "searching": false,//禁用搜索
            ajax: function (data, callback, settings) {
                //封装请求参数
                //console.log(data);
                var param = {};
                param.limit = data.length;//页面显示记录条数，在页面显示每页显示多少项的时候
                param.start = data.start;//开始的记录序号
                param.page = (data.start / data.length) + 1;//当前页码
                param.dir = data.order[0].dir;
                param.column = data.order[0].column;
                param.Id = id;
                //console.log(param);

                //ajax请求数据
                $.ajax({
                    type: "POST",
                    url: "../FangYuan/InitializationDataTableGuanLi",
                    cache: false,  //禁用缓存
                    data: param,  //传入组装的参数
                    dataType: "json",
                    success: function (result) {
                        console.log(result);
                        //setTimeout仅为测试延迟效果
                        setTimeout(function () {
                            //封装返回数据
                            var returnData = {};
                            returnData.draw = data.draw;//这里直接自行返回了draw计数器,应该由后台返回
                            returnData.recordsTotal = result.total;//返回数据全部记录
                            returnData.recordsFiltered = result.total;//后台不实现过滤功能，每次查询均视作全部结果
                            returnData.data = result.data;//返回的数据列表
                            //console.log(returnData);
                            //调用DataTables提供的callback方法，代表数据已封装完成并传回DataTables进行渲染
                            //此时的数据需确保正确无误，异常判断应在执行此回调前自行处理完毕"orderable" : false
                            callback(returnData);
                        }, 200);
                    }
                });
            },
            "columns": [
                { "data": "Id" },
                { "data": "t_FangYuanBiao.XiaoQuMingCheng" },
                { "data": "t_ZhanQuBiao.ZhanQuMingCheng" },
                { "data": null }
            ],
            columnDefs: [{
                //   指定第最后一列
                targets: -1,
                //加下面属性 不参与排序
                sortable: false,
                render: function (data, type, row, meta) {
                    return '<button type="button" class="btn btn-outline btn-default" onclick="ShanChu(' + row.Id + ')"><i class="icon wb-pencil" aria-hidden="true"></i>删除</button>';
                }
            }]
        });
    }

    function zhanqu() {
        $.ajax({
            type: 'post',
            url: '../ZhanDui/GetZhanQu',
            async: false,
            dataType: 'json',
            success: function (date) {
                var ddl = $("#zhanduimingcheng");
                ddl.append("<option value='0'>请选择</option>");
                $(date).each(function (key) {
                    var opt = $("<option></option>").text(date[key].zhanQuMingCheng).val(date[key].id);
                    ddl.append(opt);
                })
            },
        })
    }

    function AddGuanLian() {
        var id = $("#fangyuaniid").val();
        var soushuzhanqu = jQuery("#zhanduimingcheng").val();
        if (soushuzhanqu != 0) {
            $.ajax({
                type: 'post',
                url: '../FangYuan/FangYuanGuanLianAdd',
                dataType: 'text',
                async: true,
                data: { Id: id, soushuzhanqu: soushuzhanqu },
                success: function (result) {
                    if (result == "chuwu") {
                        alert("已关联!");
                    } else {
                        if (result == "1") {
                            alert("添加成功");
                            bang();
                        } else {
                            alert("添加失败");
                        }
                    }
                }
            });
            bang();
        } else {
            alert("请选择关联战区");
        }
    }

    function ShanChu(id) {
        $.ajax({
            type: 'post',
            url: '../FangYuan/FangYuanGuanLianDel',
            dataType: 'text',
            async: true,
            data: { Id: id },
            success: function (result) {
                if (result == "1") {
                    alert("删除成功");
                    bang();
                } else {
                    alert("删除失败");
                }
            }
        });
    } 

    function morenzhi() {
        var id = $("#fangyuaniid").val();
        $.ajax({
            type: 'post',
            url: '../FangYuan/Getid',
            async: false,
            dataType: 'json',
            data: { id: id },
            success: function (datea) {
                $("#xiaoqumingcheng").val(datea.XiaoQuMingCheng);
            },
        })
    }

    function Update() {
        var id = $("#fangyuaniid").val();
        var xiaoqumingcheng = $("#xiaoqumingcheng").val();
        if (xiaoqumingcheng == "") {
            alert("战队名称不能为空");
        }   else {
            $.ajax({
                type: 'post',
                url: '../FangYuan/Update',
                dataType: 'text',
                async: true,
                data: { id: id, xiaoqumingcheng: xiaoqumingcheng},
                success: function (result) {
                    if (result == "1") {
                        alert("修改成功");
                        bang();
                    } else {
                        alert("修改失败");
                    }
                }
            });
        }
    }

    function getQuery(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURI(r[2]); return null;
    }

</script>